/*
 * Copyright (C) 2011 The Code Bakers
 * Authors: Cleuton Sampaio e Francisco Rodrigues
 * e-mail: thecodebakers@gmail.com
 * Project: http://code.google.com/p/android-sensor-demo1
 * Site: http://thecodebakers.blogspot.com
 *
 * Licensed under the GNU GPL, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://gplv3.fsf.org/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * @author Cleuton Sampaio e Francisco Rogrigues - thecodebakers@gmail.com
 */

package br.com.thecodebakers.sensor.demo01;

import android.app.Activity;


import android.hardware.Sensor; // real device
import android.hardware.SensorEvent; // real device
import android.hardware.SensorEventListener; // real device


import android.hardware.SensorManager;

/*
import org.openintents.sensorsimulator.hardware.Sensor; // SensorSimulator
import org.openintents.sensorsimulator.hardware.SensorEvent; // SensorSimulator
import org.openintents.sensorsimulator.hardware.SensorEventListener; // SensorSimulator
import org.openintents.sensorsimulator.hardware.SensorManagerSimulator; // SensorSimulator
*/

import android.os.Bundle;
import android.widget.TextView;

/*
 * O programa está configurado para usar o dispositivo. Se quiser usar o SensorSimulator, 
 * comente as linhas marcadas como "Real device" e descomente as marcadas como "SensorSimulator"
 */

public class Principal extends Activity implements SensorEventListener {
	
	private SensorManager sensorManager; // real device
	//private SensorManagerSimulator sensorManager; // SensorSimulator
    private Sensor accelerometer;
    private TextView txtAcc;
    private TextView txtX;
    private TextView txtY;
    private TextView txtZ;
     
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE); // Real device
        //sensorManager =  SensorManagerSimulator.getSystemService(this, SENSOR_SERVICE); // SensorSimulator
        accelerometer  = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
        //sensorManager.connectSimulator(); // SensorSimulator
        setContentView(R.layout.main);
        txtAcc = (TextView) this.findViewById(R.id.txtAccuracy);
        txtX   = (TextView) this.findViewById(R.id.txtX);
        txtY   = (TextView) this.findViewById(R.id.txtY);
        txtZ   = (TextView) this.findViewById(R.id.txtZ);	
    }

	public void onAccuracyChanged(Sensor arg0, int arg1) {
		// Mudou a acurácia do sensor
		txtAcc.setText("= " + arg1);
	}

	public void onSensorChanged(SensorEvent event) {
		// Houve alguma aceleração no sensor
		// Normalmente, é preciso descontar a gravidade... Mas vamos deixar para depois
		txtX.setText("= " + event.values[0]);
		txtY.setText("= " + event.values[1]);
		txtZ.setText("= " + event.values[2]);
	}

	@Override
	protected void onPause() {
		// Vamos economizar baterias, pois a activity não está com o foco
		super.onPause();
		sensorManager.unregisterListener(this); // deixa de receber avisos
	}

	@Override
	protected void onResume() {
		// Temos que reiniciar o sensor
		super.onResume();
		sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	
}